package com.whcoding.email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 发送邮件
 */
@RestController
@RequestMapping("/email")
public class SendEmailTest {
    @Autowired
	JavaMailSenderImpl mailSender;

    @GetMapping("/sendEasyEmail")
    public String sendMailTest() {
        SimpleMailMessage simpleMail = new SimpleMailMessage();
        simpleMail.setSubject("SnailThink Hello");
        simpleMail.setText("感谢");
        simpleMail.setTo("137299954@qq.com");
        simpleMail.setFrom("137299954@qq.com");
        mailSender.send(simpleMail);
        System.out.println("发送邮件完成");
        return "邮件发送完成";
    }


    @GetMapping("/sendHardEmail")
    public String sendComplexMailTest() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //正文
        helper.setSubject("snailThink 你好呀~~~");
        helper.setText("<p style='color:red'>今天又是努力的一天奥利给</p>", true);
        //附件
        helper.addAttachment("1.jpg", new File("C:\\Users\\Manager\\Desktop\\1.jpg"));
        helper.addAttachment("视频知识点.md", new File("C:\\Users\\Manager\\Desktop\\视频知识点.md"));
        //抄送
        helper.addCc("3188655855@qq.com");
        helper.setTo("137299954@qq.com");
        helper.setFrom("137299954@qq.com");
        mailSender.send(mimeMessage);
        System.out.println("发送邮件完成");
        return "复杂发送邮件完成";
    }
}
